package com.jiyinit.web.core.activiti.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.jiyinit.web.core.activiti.enums.ProcessFlowEnum;
import com.jiyinit.web.core.activiti.enums.evection.EveTaskGatewayEnum;
import com.jiyinit.web.core.activiti.vo.EveReviewVo;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;

import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.web.core.activiti.dao.TabCoreEvectionDao;
import com.jiyinit.web.core.activiti.dto.EvectionRequest;
import com.jiyinit.web.core.activiti.entity.TabCoreEvectionEntity;

@Service("core.evectionServiceImpl")
public class EvectionServiceImpl {

    @Resource(name = "core.tabCoreEvectionDao")
    private TabCoreEvectionDao tabCoreEvectionDao;

    @Resource(name = "runtimeService")
    RuntimeService runtimeservice;
    @Resource(name = "identityService")
    IdentityService identityservice;
    @Resource(name = "taskService")
    TaskService taskservice;


    /**
     * 多条件分页查询
     *
     * @param request
     * @return
     */
    public PagingResult<TabCoreEvectionEntity> findForPage(EvectionRequest request) {
        return tabCoreEvectionDao.findForPage(request);
    }

    /**
     * id查询
     *
     * @param id
     * @return
     */
    public TabCoreEvectionEntity findOne(Integer id) {
        return tabCoreEvectionDao.findOne(id);
    }

    /**
     * 添加
     *
     * @param entity
     * @return
     */
    public int add(TabCoreEvectionEntity entity) {
        return tabCoreEvectionDao.add(entity);
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    public int update(TabCoreEvectionEntity entity) {
        return tabCoreEvectionDao.update(entity);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int delete(Integer id) {
        return tabCoreEvectionDao.delete(id);
    }


    /**
     * 获取当前用户的待提交的最大id,即为最新id
     *
     * @param userId
     * @return
     */
    public int getMaxIdByUserId(Integer userId) {
        return tabCoreEvectionDao.getMaxIdByUserId(userId);
    }
    /*txd 添加end*/

    public List<TabCoreEvectionEntity> findBySelective(TabCoreEvectionEntity record) {
        return tabCoreEvectionDao.findBySelective(record);
    }


    public ProcessInstance startWorkflow(TabCoreEvectionEntity billInfoEntity, String userid) {
        String businesskey = String.valueOf(billInfoEntity.getId());//使用leaveapply表的主键作为businesskey,连接业务数据和流程数据
        Map<String, Object> variables = new HashMap<String, Object>();
        this.setProcessVariables(variables, billInfoEntity);
        identityservice.setAuthenticatedUserId(userid);  //设置用户id
        ProcessInstance instance = runtimeservice.startProcessInstanceByKey(ProcessFlowEnum.Evection.getCode(), businesskey, variables);
        String instanceid = instance.getId();
        billInfoEntity.setProcessId(instanceid);
        billInfoEntity.setModifyTime(new Date());
        this.update(billInfoEntity);
        return instance;
    }

    //设置全局变量
    private void setProcessVariables(Map<String, Object> variables, TabCoreEvectionEntity billInfoEntity) {
        TabCoreEvectionEntity record = billInfoEntity;
        // 设置节点拾取人
        variables.put("eveUserId", record.getUserId() + "");   //设置全局变量方便后面节点调用

        String gateway1 = "false";
        Integer userLeavel = record.getUserLeavel();
        if(userLeavel.intValue() == 2){
            gateway1 = "true";
        }
        //设置后续节点参数
        variables.put(EveTaskGatewayEnum.Task1Gateway1.getGatewayCode(), gateway1);  //设置身份
        //设置部门
        String orgsFlag = "1";
        variables.put(EveTaskGatewayEnum.Task1Gateway2.getGatewayCode(), orgsFlag);  //设置部门
        if ("2".equals(record.getUserLeavel())) {
            variables.put(EveTaskGatewayEnum.Task1Gateway2.getGatewayCode(), "2");  //设置部门
        }
    }

    public List<EveReviewVo> getDealTasksByUserId(Integer userId){
        return tabCoreEvectionDao.getDealTasksByUserId(userId);
    }

}
